package com.kandinfo.survey.weixin.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kandinfo.base.common.dto.ResultDto;
import com.kandinfo.base.common.dto.ResultDtoFactory;
import com.kandinfo.base.common.dto.annotation.OnValid;
import com.kandinfo.base.security.dto.UserInfoDto;
import com.kandinfo.survey.weixin.dto.UserRegistDto;
import com.kandinfo.survey.weixin.service.IWxUserService;

/**
 * Description: 个人管理，包括注册、修改密码、登录
 * 
 * @author Patrick Chenzp-TG
 * @version 1.0
 */
@RequestMapping("/weixin/usermanage")
@Controller
public class UserManageController {

    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(UserManageController.class);

    @Autowired
    private IWxUserService wxUserService;

    /**
     * 获取用户中心页面数据
     */
    @RequestMapping(value = "/get-user-center-data", method = RequestMethod.GET)
    public ResultDto getUserCenterData(Model model) {
    	// TODO 待修改
        return ResultDtoFactory.toAckNomessage(wxUserService.getUserInfo());
    }

    /**
     * 用户信息页和信息修改页
     */
    @RequestMapping(value = "/user-info", method = RequestMethod.GET)
    public String userInfo(Model model) {
        return "/weixin/user/user-info";
    }
    /**
     * 用户信息页和信息修改页
     */
    @RequestMapping(value = "/get-user-info", method = RequestMethod.GET)
    @ResponseBody
    public ResultDto getUserInfo() {
        return ResultDtoFactory.toAckNomessage(wxUserService.getUserInfo());
    }
    /**
     * 补充用户资料
     * 
     * @return
     */
    @RequestMapping(value = "/complete-user", method = RequestMethod.POST)
    @ResponseBody
    public ResultDto completeUser(@RequestBody UserInfoDto userInfoDto) {
        return wxUserService.completeUser(userInfoDto);
    }
    

    /**
     * 用户注册页面
     */
    @RequestMapping(value = "/regist", method = RequestMethod.GET)
    public String regist(Model model) {
        return "/weixin/user/user-regist";
    }
    /**
     * 执行用户注册
     */
    @RequestMapping(value = "/do-regist", method = RequestMethod.POST)
    @ResponseBody
    public ResultDto doRegist(@OnValid @RequestBody UserRegistDto userRegistDto) {
        return wxUserService.doRegist(userRegistDto);
    }

    /**
     * 密码修改页面
     */
    @RequestMapping(value = "/modify-passwd", method = RequestMethod.GET)
    public String modflyPasswd(Model model) {
        return "/weixin/user/modify-passwd";
    }
    /**
     * 修改密码
     */
    @RequestMapping(value = "/do-modify-passwd", method = RequestMethod.POST)
    @ResponseBody
    public ResultDto doModflyPasswd(@RequestBody @RequestParam String oldPass, @RequestParam String nowPass) {
        return wxUserService.changePwdAndRedirect(oldPass, nowPass);
    }

    /**
     * 查看原密码是否正确
     * 
     * @param oldPass
     * @return
     */
    @RequestMapping(value = "/check-old-pass", method = RequestMethod.POST)
    @ResponseBody
    public ResultDto checkOldPass(@RequestParam(name = "oldPass") String oldPass) {
        return wxUserService.checkOldPass(oldPass);
    }

    /**
     * 查看用户资料是否补充完全
     * 
     * @return
     */
    @RequestMapping(value = "/check-is-complete", method = RequestMethod.POST)
    @ResponseBody
    public ResultDto checkIsComplete(@RequestParam Long userId) {
        Boolean flag = wxUserService.checkIsComplete(userId);
        if (flag) {
            return ResultDtoFactory.toAck("信息完全");
        }
        return ResultDtoFactory.toNack("信息未完全");
    }

}
